package com.example.workoutjournal;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DAO {

  // Database fields
  private SQLiteDatabase database;
  private MySQLiteHelper dbHelper;
  private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
      MySQLiteHelper.COLUMN_SETNAME };
  
  private String[] allColumnsEx = { MySQLiteHelper.COLUMN_ID,
	      MySQLiteHelper.COLUMN_SETID, MySQLiteHelper.COLUMN_EXERCISENAME, MySQLiteHelper.COLUMN_REPETITIONS, MySQLiteHelper.COLUMN_SERIES, MySQLiteHelper.COLUMN_WEIGHT };

  public DAO(Context context) {
    dbHelper = new MySQLiteHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public Set createSet(String setName) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_SETNAME, setName);
    long insertId = database.insert(MySQLiteHelper.TABLE_SETS, null,
        values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_SETS,
        allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
        null, null, null);
    cursor.moveToFirst();
    Set newSet = cursorToSet(cursor);
    cursor.close();
    return newSet;
  }
  
  public Exercise createExercise(String exerciseName, Integer repetitions, Integer weight, Integer sets, Integer setID){
	  ContentValues values = new ContentValues();
	  values.put(MySQLiteHelper.COLUMN_EXERCISENAME, exerciseName);
	  values.put(MySQLiteHelper.COLUMN_REPETITIONS, repetitions);
	  values.put(MySQLiteHelper.COLUMN_WEIGHT, weight);
	  values.put(MySQLiteHelper.COLUMN_SERIES, sets);
	  values.put(MySQLiteHelper.COLUMN_SETID, setID);
	  
	  long insertId = database.insert(MySQLiteHelper.TABLE_EXERCISES, null, values);
	  Cursor cursor = database.query(MySQLiteHelper.TABLE_EXERCISES, allColumnsEx, MySQLiteHelper.COLUMN_ID + " = " + insertId, null, null, null, null);
	  cursor.moveToFirst();
	  Exercise newExercise = cursorToExercise(cursor);
	  cursor.close();
	  return newExercise;
  }

  /*public void deleteComment(Comment comment) {
    long id = comment.getId();
    System.out.println("Comment deleted with id: " + id);
    database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
        + " = " + id, null);
  }*/

  public List<Set> getAllComments() {
    List<Set> sets = new ArrayList<Set>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_SETS,
        allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Set set = cursorToSet(cursor);
      sets.add(set);
      cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return sets;
  }
  
  public Integer deleteSet(Integer setID){
	  database.delete(MySQLiteHelper.TABLE_EXERCISES, MySQLiteHelper.COLUMN_SETID+" = ?", new String[] { setID.toString() });
	  Integer rows = database.delete(MySQLiteHelper.TABLE_SETS, MySQLiteHelper.COLUMN_ID+" = ?", new String[] { setID.toString() });
	  return rows;
  }
  
  public Integer deleteExercise(Integer exID){
	  Integer rows = database.delete(MySQLiteHelper.TABLE_EXERCISES, MySQLiteHelper.COLUMN_ID+" = ?", new String[] { exID.toString() });
	  return rows;
  }
  
  public List<Exercise> getAllExercises(Integer setID){
	    List<Exercise> exercises = new ArrayList<Exercise>();

	    Cursor cursor = database.query(MySQLiteHelper.TABLE_EXERCISES,
	        allColumnsEx, "setid = "+setID, null, null, null, null);

	    cursor.moveToFirst();
	    while (!cursor.isAfterLast()) {
	      Exercise exercise = cursorToExercise(cursor);
	      exercises.add(exercise);
	      cursor.moveToNext();
	    }
	    // make sure to close the cursor
	    cursor.close();
	    return exercises;
  }
  
  public Exercise getExercise(Integer exID){
	  Cursor cursor = database.query(MySQLiteHelper.TABLE_EXERCISES,
		        allColumnsEx, MySQLiteHelper.COLUMN_ID+" = "+exID, null, null, null, null);
	  cursor.moveToFirst();
	  Exercise exercise = cursorToExercise(cursor);
	  
	  return exercise;
  }

  private Set cursorToSet(Cursor cursor) {
	Set set = new Set();
	set.setName(cursor.getString(1));
	set.setID(cursor.getInt(0));
	return set;
  }
  private Exercise cursorToExercise(Cursor cursor){
	  Exercise exercise = new Exercise();
	  exercise.setName(cursor.getString(2));
	  exercise.setID(cursor.getInt(0));
	  exercise.setSetID(cursor.getInt(1));
	  exercise.setRepetitions(cursor.getInt(3));
	  exercise.setWeight(cursor.getInt(5));
	  exercise.setSets(cursor.getInt(4));
	  
	  return exercise;
  }
} 